package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.database.DbUtil1;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.page.Page;

public class FamilyDao extends GenericDao<Family>{

	@Override
	protected Family handle(ResultSet rs) throws SQLException {
		String xareacode = rs.getString("xareacode");
		String zareacode = rs.getString("zareacode");
		String careacode = rs.getString("careacode");
		String housecode = rs.getString("housecode");
		String familycode = rs.getString("familycode");
		String housemaster = rs.getString("housemaster");
		String sumfamily = rs.getString("sumfamily");
		String sumfarmer = rs.getString("sumfarmer");
		String address = rs.getString("address");
		String createtime = rs.getString("createtime");
		String registrar = rs.getString("registrar");
		String areacode = rs.getString("areacode");
		String group = rs.getString("group");
		
		Family entity = new Family(xareacode,zareacode,careacode,housecode,familycode,housemaster,sumfamily,sumfarmer,address,createtime,registrar,areacode,group);
		
		return entity;
		
	}
	
	 // 通过家庭编号自动计算家庭人口数
	 public int getFamilySizeById(String familycode) throws Exception {
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			int familySize = 0;
			try{
				conn = DbUtil.getConn();
				StringBuffer sql = new StringBuffer("select count(*) from t_family where familycode=? ");
				pstmt = conn.prepareStatement(sql.toString());
				int index = 1;
				pstmt.setString(index, familycode);
				rs = pstmt.executeQuery();
				if(rs.next()){
					familySize = rs.getInt(1);
				}
			}catch(SQLException e){
				throw new SQLException("Family with id " + familycode + " could not be loaded from the database.", e);
			}finally{
				DbUtil.close(rs, pstmt, conn);
			}
			return familySize;
		}
	 

		/**
		 * 分页方法
		 * @param pageSize 每一页的行数
		 * @param pageNow 当前页
		 * @return page
		 */
		public Page<Family> queryPage(int pageSize, int pageNow) {
			DbUtil1 dbUtil = new DbUtil1();
			
			Page<Family> page = new Page<Family>();
			page.setPageSize(pageSize);
			page.setPageNow(pageNow);
			String sql = "select count(*) from t_family";	
			dbUtil.getConnection();
			ResultSet rs = dbUtil.executeQuery(sql, null);
			try {
				rs.next();
				Integer rowCount = rs.getInt(1);
				page.setRowCount(rowCount);
				List<Family> pageRow = new ArrayList<Family>();
				String preparedSql = "select * from t_family limit ?,?";
				Object[] param = { page.getPageSize() *( page.getPageNow() - 1), page.getPageSize() };
				ResultSet res = dbUtil.executeQuery(preparedSql, param);
				System.out.println("执行分页查询");
				while (res.next()) {
					Family family = new Family(res.getString("familycode"), res.getString("areacode"), res.getString("sumfamily"),res.getString("sumfarmer"),res.getString("address"),
							res.getString("createtime"), res.getString("registrar"), res.getString("housemaster"),res.getString("housecode"),res.getString("xareacode"),res.getString("zareacode"), res.getString("careacode"),res.getString("group"));									
					pageRow.add(family);
				}
				//System.out.println("pageRow:"+pageRow);
				page.setpageRow(pageRow);
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				dbUtil.closeAll();
			}
			return page;
		}
}
